package com.ls.utils.http;


import com.alibaba.fastjson.JSONObject;
import com.ls.utils.JsonUtils;
import okhttp3.*;

import java.io.IOException;


/**
 * @ClassName HttpUtil
 * @Description OKHttp工具类
 * @Author TangQuanBin
 * @Date 2019/9/9 18:35
 * @Version 1.0
 **/
public class NoProxyHttpUtils {

    private static int connectTimeOut = 100;
    private static int readTimeout = 100;
    private static int writeTimeout = 100;


    /**
     * json方式发送Post请求
     *
     * @param url   请求URL
     * @param param 传入JSONObject
     * @return 返回结果
     * @throws IOException
     */
    public static String sendPost(String url, JSONObject param) throws IOException {
        return sendPost(url, JsonUtils.toJsonString(param));
    }


    /**
     * json方式发送Post请求
     *
     * @param url   请求URL
     * @param param 传入已经转换成json字符串
     * @return 返回结果
     * @throws IOException
     */
    public static String sendPost(String url, String param) throws IOException {

        OkHttpClient okHttpClient = NoProxyOkHttpHelper.getInstance().getOkHttpClient();

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, param);

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("Content-Type", "application/json")
                .addHeader("Cache-Control", "no-cache")
                .build();

        Response response = okHttpClient.newCall(request).execute();
        String result = response.body().string();
        return result;

    }
    /**
     * xml方式发送Post请求
     *
     * @param url   请求URL
     * @param param 传入已经转换成json字符串
     * @return 返回结果
     * @throws IOException
     */
    public static String sendXmlPost(String url, String param) throws IOException {

        OkHttpClient okHttpClient = NoProxyOkHttpHelper.getInstance().getOkHttpClient();

        MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded;charset=utf-8");
        RequestBody body = RequestBody.create(mediaType, param);

        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
                .addHeader("Cache-Control", "no-cache")
                .build();

        Response response = okHttpClient.newCall(request).execute();
        String result = response.body().string();
        return result;

    }
    /**
     * 发送 GET 同步请求
     *
     * @param url 请求URL
     * @return 返回结果
     * @throws IOException
     */
    public static String sendSyncGet(String url) throws IOException {
        OkHttpClient okHttpClient = NoProxyOkHttpHelper.getInstance().getOkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        Response response = call.execute();
        return response.body().string();
    }

    /**
     * 发送 GET 异步请求
     *
     * @param url      url地址
     * @param callback 传入回调
     * @throws IOException
     */
    public static void sendAsyncGet(String url, Callback callback) throws IOException {

        OkHttpClient okHttpClient = NoProxyOkHttpHelper.getInstance().getOkHttpClient();

        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = okHttpClient.newCall(request);
        //设置异步回调
        call.enqueue(callback);
    }
}
